using SkiaSharp;
using SkiaSharp.QrCode;
using SkiaSharp.QrCode.Image;

namespace Vin.Extension.ImageSharp.Extension;

public static class QrcodeExtension
{
    public static Stream CreateQrcode(this string content, int width, int height, ECCLevel eccLevel = ECCLevel.H,
        bool forceUtf8 = false,
        bool utf8BOM = false, QRCodeGenerator.EciMode eciMode = QRCodeGenerator.EciMode.Default,
        int requestedVersion = -1, int quietZoneSize = 1, SKEncodedImageFormat format = SKEncodedImageFormat.Png,
        int quality = 300)
    {
        using var generator = new QRCodeGenerator();
        var qrCodeData = generator.CreateQrCode(content, eccLevel, forceUtf8, utf8BOM, eciMode,
            requestedVersion, quietZoneSize);
        var skImg = new SKImageInfo(width, height);
        using var surface = SKSurface.Create(skImg);
        using var canvas = surface.Canvas;
        canvas.Clear(SKColors.White);
        canvas.Render(qrCodeData, width, height);
        using var image = surface.Snapshot();
        var data = image.Encode(format, quality);
        return data.AsStream();
    }
}